package indi.zhifa.recipe.bailan.framework.auth.dao.service.impl;

import com.alibaba.fastjson2.JSONObject;
import indi.zhifa.recipe.bailan.framework.auth.dao.service.IRelUserRoleDbService;
import indi.zhifa.recipe.bailan.framework.auth.dao.service.IRoleDbService;
import indi.zhifa.recipe.bailan.framework.auth.dao.service.IUserDbService;
import indi.zhifa.recipe.bailan.framework.auth.dao.service.IUserRelevantDao;
import indi.zhifa.recipe.bailan.framework.auth.entity.po.*;
import indi.zhifa.recipe.bailan.framework.common.entity.exception.ServiceException;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@RequiredArgsConstructor
@Service
public class UserRelevantDaoImpl implements IUserRelevantDao {

    private final IUserDbService mUserDbService;
    private final IRoleDbService mRoleDbService;
    private final IRelUserRoleDbService mRelUserRoleDbService;

    @Override
    public UserEntity user_generate() {
        return new UserEntity();
    }

    @Override
    public UserEntity user_check(Long id) {
        return mUserDbService.check(id);
    }

    @Override
    public Long user_findByUserName(String pUserName) {
        return mUserDbService.findByUserName(pUserName);
    }

    protected UserEntity checkUserEntity(BaseUserEntity pBaseUserEntity){
        if(! (pBaseUserEntity instanceof UserEntity)){
            throw new ServiceException("存储用户数据必须是UserEntity，不能是基类");
        }
        return (UserEntity)pBaseUserEntity;
    }

    @Override
    public UserEntity user_save(BaseUserEntity pBaseUserEntity) {
        return mUserDbService.savePull(pBaseUserEntity.getId(),checkUserEntity(pBaseUserEntity));
    }

    @Override
    public UserEntity user_edit(Long pId, BaseUserEntity pBaseUserEntity) {
        return mUserDbService.updatePull(pId,checkUserEntity(pBaseUserEntity));
    }

    @Override
    public UserEntity user_removeById(Long pId) {
        return mUserDbService.deleteById(pId);
    }

    @Override
    public List<BaseRoleEntity> role_list() {
        return new ArrayList(mRoleDbService.list());
    }

    @Override
    public Long role_findByCode(String pCode) {
        return mRoleDbService.findByCode(pCode);
    }

    @Override
    public RoleEntity role_check(Long pId) {
        return mRoleDbService.check(pId);
    }

    protected RoleEntity checkRoleEntity(BaseRoleEntity pRoleEntity){
        if(! (pRoleEntity instanceof RoleEntity)){
            throw new ServiceException("存储用户数据必须是UserEntity，不能是基类");
        }
        return (RoleEntity)pRoleEntity;
    }

    @Override
    public RoleEntity role_save(BaseRoleEntity pBaseRoleEntity) {
        RoleEntity roleEntity = checkRoleEntity(pBaseRoleEntity);
        mRoleDbService.savePull(roleEntity.getId(),roleEntity);
        return roleEntity;
    }

    @Override
    public RelUserRoleEntity relUserRole_generate() {
        RelUserRoleEntity relUserRoleEntity = new RelUserRoleEntity();
        return relUserRoleEntity;
    }

    @Override
    public List<BaseRelUserRoleEntity> relUserRole_listByUserId(Long pUserId) {
        return new ArrayList<>(mRelUserRoleDbService.listByUserId(pUserId));
    }

    @Override
    public boolean relUserRole_updateRoles(Long pUserId, List<BaseRelUserRoleEntity> relUserRoleEntityList) {
        List<RelUserRoleEntity> roleEntityList = new ArrayList<>();
        for(BaseRelUserRoleEntity brure : relUserRoleEntityList){
            roleEntityList.add((RelUserRoleEntity)brure);
        }
        mRelUserRoleDbService.edit(pUserId,roleEntityList);
        return true;
    }

    @Override
    public UserEntity user_load(JSONObject pUserCfg) {
        UserEntity userEntity = pUserCfg.to(UserEntity.class);
        userEntity.createInit();
        return userEntity;
    }

    @Override
    public RoleEntity role_load(JSONObject pRoleCfg) {
        RoleEntity roleEntity = pRoleCfg.to(RoleEntity.class);
        roleEntity.createInit();
        return roleEntity;
    }

}
